﻿@{
    ViewBag.Title = "公告发布";
}
<style>
    .btn-group {
        margin-top: 0px !important;
        text-align: left !important;
    }
</style>
<div class="container" style="margin-top:30px;margin-bottom:20px">
    <div class="row">
        <form class="form-horizontal" id="mainForm">

            <div class="form-group ">
                <label for="Title" class="col-sm-2 control-label"><em class="required">*</em>公告标题：</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="Title" id="Title" placeholder="请输入公告标题" />
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 col-sm-2 control-label"><em class="required">*</em>公告内容 : </label>
                <div class="col-sm-8 ">
                    <textarea name="Content" id="Content" class="form-control" style="height:120px;" placeholder="请输入公告内容"></textarea>
                    <small class="help-block" data-bv-validator="notEmpty" data-bv-for="Content" data-bv-result="NOT_VALIDATED" style="display: none;">公告内容不能为空</small>
                </div>
            </div>
            <div class="form-group">
                <label for="NoticePorjectName" class="col-sm-2 control-label">接收项目：</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" autocomplete="off"  id="ProjectNames" name="ProjectNames" value="" placeholder="请选择接收项目">
                    <input type="hidden" class="form-control" id="ProjectIds" name="ProjectIds" value="">
                </div>
            </div>
            <div class="form-group ">
                <label for="NoticeCompanyName" class="col-sm-2 control-label">接收单位：</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" autocomplete="off" id="CompanyNames" name="CompanyNames" value="" placeholder="请选择接收单位">
                    <input type="hidden" class="form-control" id="CompanyIds" name="CompanyIds" value="">
                </div>
            </div>
            <div class="form-group ">
                <label for="NoticeUserName" class="col-sm-2 control-label">接收人员：</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" id="UserNames" autocomplete="off"  name="UserNames" value="" placeholder="请选择接收人员">
                    <input type="hidden" class="form-control" id="UserIds" name="UserIds" value="">
                </div>
            </div>
            <div class="form-group ">
                <label for="WayOfRelease" class="col-sm-2 control-label"><em class="required">*</em>发布途径：</label>
                <div class="col-sm-8" style="padding-top:6px">
                    @*发布途径0无 1PC 2APP 3PC和APP*@
                    <label for="WayOfRelease_1">PC</label>
                    <input type="checkbox" lay-skin="primary" id="WayOfRelease_1" checked />
                    <label for="WayOfRelease_2">APP</label>
                    <input type="checkbox" lay-skin="primary" id="WayOfRelease_2" checked/>
                    <input type="hidden" id="WayOfRelease" name="WayOfRelease" />
                </div>
            </div>
            <div class="col-lg-12 col-sm-12 mb25" style="margin-top:57px">
                <div class="col-lg-6 col-sm-6" style="text-align:right">
                    <input type="button" name="submit" id="save" lay-submit lay-filter="sub" value="提&nbsp;&nbsp;&nbsp;交" class="layui-btn layui-btn-small button-base bg-save" />
                </div>
                <div class="col-lg-6 col-sm-6">
                    <input type="button" name="cacel" value="取&nbsp;&nbsp;&nbsp;消" class="layui-btn layui-btn-small button-base bg-cancel cancel" />
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">
    var saveAjax = function () {
        if ($("#WayOfRelease_1").prop("checked") && $("#WayOfRelease_2").prop("checked")) {
            $("#WayOfRelease").val("3");
        }
        else if ($("#WayOfRelease_1").prop("checked")) {

            $("#WayOfRelease").val("1");
        }
        else if ($("#WayOfRelease_2").prop("checked")) {

            $("#WayOfRelease").val("2");
        }
        else {
            $("#WayOfRelease").val("0");
        }
        var formData = $("#mainForm").serializeArray();
        $.ajax({
            url: "/Notice/Add",
            type: "post",
            data: formData,
            dataType: "json",
            success: function (data) {
                if (data.Flag) {
                    parent.layer.msg("添加成功", { time: 1000, icon: 1 }, function () {
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);//关闭窗口
                        parent.layer.close(layerIndex);
                        parent.window.frames["mainFrame"].location.reload();
                    });
                } else {
                    layer.alert(data.Message, { icon: 5 });
                }
            },
            error: function () { alert("请求出错"); }
        });
    };
    window.onload = function () {
        //自定义验证规则
        $('#mainForm').bootstrapValidator({
            excluded: [':hidden'],//[':disabled', ':hidden', ':not(:visible)'] //设置隐藏组件可验证
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                Title: {
                    validators: {
                        notEmpty: {
                            message: '公告标题不能为空'
                        },
                        stringLength: {
                            max: 50,
                            message: '公告标题长度不能超过50字符'
                        },
                    }
                },
                Content: {
                    validators: {
                        notEmpty: {
                            message: '公告内容不能为空'
                        },
                        stringLength: {
                            max: 250,
                            message: '公告内容长度不能超过250字符'
                        },
                    }
                },
            }
        });
    }
    layui.use(['layer', 'form', 'element', 'laypage'], function () {

        var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
        //监听提交
        form.on('submit(sub)', function (data) {
      
            //对表单进行验证
            var bv = $('#mainForm').data('bootstrapValidator');
            bv.validate();
            if (bv.isValid()) {
                if ($('#ProjectNames').val() == "" && $('#CompanyNames').val() == "" && $('#UserNames').val() == "") {
                    parent.layer.alert("接受单位，人员，项目必须填一个。", { icon: 5 });
                    return false;
                }
                if (!$("#WayOfRelease_1").prop("checked") && !$("#WayOfRelease_2").prop("checked")) {
                    parent.layer.alert("发布途径不可为空!", { icon: 5 });
                    return false;
                }
                saveAjax();
                return true;
            } 

            return false;
        });

        //关闭弹出iframe层
        $('.cancel').on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);//关闭窗口
        });
    });
    //$("#ProjectNames").click(function () {
    //    layer.open({
    //        type: 2,
    //        title: GetLayerTitle("公告管理-选择项目"),
    //        area: [BigLayerWidth, '700px'],
    //        content: ['/Common/SelectProject?IsMultiple=true', 'no'],
    //        end: function () {
    //            console.log($("#NoticePorjectName").val());
    //        }
    //    });
    //});

    //$("#CompanyNames").click(function () {
    //    layer.open({
    //        type: 2,
    //        title: GetLayerTitle("公告管理-选择单位"),
    //        area: [BigLayerWidth, '700px'],
    //        content: '/Common/SelectCompany?IsMultiple=true',
    //        end: function () {
    //            console.log($("#NoticeCompanyId").val());
    //        }
    //    });
    //});
    $("#UserNames").click(function () {
        layer.open({
            type: 2,
            title: GetLayerTitle("公告管理-选择用户"),
            area: [BigLayerWidth, '700px'],
            content: ['/Common/SelectUser?IsMultiple=true', 'no'],
            end: function () {
                console.log($("#NoticeUserIDs").val());
            }
        });
    });
    var setComanyValue = function (names, ids) {
        $("#CompanyNames").val(names);
        $("#CompanyIds").val(ids);
    }
    var setUserValue = function (names, ids) {
        $("#UserNames").val(names);
        $("#UserIds").val(ids);
    }
    var setProjectValue = function (names, ids) {
        $("#ProjectNames").val(names);
        $("#ProjectIds").val(ids);
    }

</script>